package com.lse_api.utils;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;

/**
 * 文件上传通用类
 *
 * @author bcl
 */
public class FileUpLoadUtil {

    /**
     * 通用文件上传1
     *
     * @param file
     * @param request
     * @param floadName      = 文件夹名称
     * @param propertiesName = 系统配置文件名称
     *                       [配置文件手动新建,在配置文件中加入参数作为上传文件后返回的前半部分地址]
     * @param httpKey        = 系统配置文件中http的定义
     * @return
     * @throws IOException
     */
    public static Map<String, Object> fileUpload(MultipartFile file,
                                                 HttpServletRequest request,
                                                 String floadName,
                                                 String propertiesName,
                                                 String httpKey)
            throws IOException {
        String path = request.getSession().getServletContext().getRealPath(floadName);
        String fileName = file.getOriginalFilename();
        File dir = new File(path, fileName);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        file.transferTo(dir);
        Map<String, Object> map = new HashMap<String, Object>();
        String httpUrl = getProperties(propertiesName, httpKey);
        map.put("url", httpUrl + floadName + File.separator + fileName);
        map.put("fileName", fileName);
        map.put("floadName", floadName);
        return map;
    }

    /**
     * 通用文件上传2 用于ajax上传formdata数据
     *
     * @param file
     * @param request
     * @param floadName      = 文件夹名称
     * @param propertiesName = 系统配置文件名称
     *                       [配置文件手动新建,在配置文件中加入参数作为上传文件后返回的前半部分地址]
     * @param httpKey        = 系统配置文件中http的定义
     * @return
     * @throws IOException
     */
    public static Map<String, Object> fileUploadAjaxFormdata(MultipartFile file,
                                                             HttpServletRequest request,
                                                             String floadName,
                                                             String propertiesName,
                                                             String httpKey)
            throws IOException {
        String path = request.getSession().getServletContext().getRealPath(floadName);
        String fileName = file.getOriginalFilename();
        //String fileName = request.getParameter("name");
		/*if (fileName == null || fileName == "") {
			fileName = file.getOriginalFilename() ;
		}*/
        File dir = new File(path, fileName);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        file.transferTo(dir);
        Map<String, Object> map = new HashMap<String, Object>();
        String httpUrl = getProperties(propertiesName, httpKey);
        map.put("url", httpUrl + floadName + File.separator + fileName);
        map.put("fileName", fileName);
        map.put("floadName", floadName);
        return map;
    }

    /**
     * 通用文件上传 - 公共默认上传
     *
     * @param file
     * @param request
     * @param url        文件访问地址(不包含文件名称)
     * @param fileName
     * @param folderName
     * @return
     * @throws IOException
     */
    public static Map<String, String> fileUpByDefault(MultipartFile file, HttpServletRequest request, String url, String fileName, String folderName) throws IOException {

        String path = request.getSession().getServletContext().getRealPath(folderName);

        if (fileName == null || "".equals(fileName)) {
            fileName = file.getOriginalFilename();
        }

        File dir = new File(path, fileName);
        if (!dir.exists()) {
            dir.mkdirs();
        }

        file.transferTo(dir);
        Map<String, String> map = new HashMap<>();
        map.put("url", url + File.separator + fileName);
        map.put("fileName", fileName);
        map.put("folderName", folderName);
        map.put("path", path);

        return map;

    }

    /**
     * 通用文件上传 - 公共默认上传 - springboot
     *
     * @param file
     * @param fileName
     * @param folderName
     * @return
     * @throws IOException
     */
    public static boolean fileUpByDefault_(MultipartFile file, String fileName, String folderName, String filePath) throws IOException {

        if (fileName == null || "".equals(fileName)) {
            fileName = file.getOriginalFilename();
        }

        /*File path = new File(ResourceUtils.getURL("classpath:").getPath());

        if (!path.exists()) {
            path = new File("");
        }*/

        String uploadPath = filePath + File.separator + folderName;

        System.out.println(uploadPath);

        File dir = new File(uploadPath, fileName);
        if (!dir.getParentFile().exists()) {
            dir.getParentFile().mkdirs();
        }

        file.transferTo(dir);

        return dir.exists();

    }

    /**
     * <p>用于删除文件</p>
     *
     * @param FileName  ==	被删除的文件名
     * @param request
     * @param floadName ==	被删除的文件位于哪个文件夹下
     * @return
     */
    public static Map<String, Object> deleteFile(String FileName,
                                                 HttpServletRequest request,
                                                 String floadName) {
        Map<String, Object> map = new HashMap<String, Object>();
        Integer code = 200;
        String value = "删除成功";
        try {
            String path = request.getSession().getServletContext().getRealPath(floadName);
            File file = new File(path + File.separator + FileName);
            if (file.exists()) {
                boolean flag = file.delete();
                if (!flag) {
                    code = 500;
                    value = "删除失败";
                } else {
                    code = 200;
                    value = "删除成功";
                }
            } else {
                code = 404;
                value = "不好意思,亲   没有找到该文件哟";
            }
        } catch (Exception e) {
            code = 500;
            value = "删除过程中出现错误,删除失败";
        }
        map.put("code", code);
        map.put("msg", value);
        return map;
    }

    /**
     * <p>读取properties配置文件信息  - 读取多个,返回数组</p>
     *
     * @param propertiesName = 配置文件名称
     * @param key            = 文件中每个变量的名称
     * @return
     */
    public static String[] getProperties(String propertiesName, String[] key) {
        ResourceBundle resourceBundle = ResourceBundle.getBundle(propertiesName);
        String[] value = new String[key.length];
        for (int i = 0; i < key.length; i++) {
            value[i] = resourceBundle.getString(key[i]);
        }
        return value;
    }

    /**
     * <p>读取properties配置文件信息  - 读取单个,返回值</p>
     *
     * @param propertiesName = 配置文件名称
     * @param key            = 文件中变量的名称
     * @return
     */
    public static String getProperties(String propertiesName, String key) {
        ResourceBundle resourceBundle = ResourceBundle.getBundle(propertiesName);
        String value = resourceBundle.getString(key);
        return value;
    }

    public static void main(String[] args) {

    }

}
